package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.user.dto.LoginDto;
import com.heima.user.entity.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.IApUserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * APP用户信息表 服务实现类
 * </p>
 *
 * @author Wq
 * @since 2022-10-06
 */
@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements IApUserService {

    @Override
    public ResponseResult login(LoginDto dto) {
        //1、参数校验
        if (StringUtils.isBlank(dto.getEquipmentId()) || dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2、判断是用户登录还是不登录先查看
        if (StringUtils.isNotBlank(dto.getPhone()) && StringUtils.isNotBlank(dto.getPassword())) {
            //2.1 app用户登录
            LambdaQueryWrapper<ApUser> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ApUser::getPhone, dto.getPhone());
            ApUser apUser_DB = this.getOne(wrapper);
            if (apUser_DB == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.USER_NOT_EXIST);
            }
            //比对密码是否正确
            String password = DigestUtils.md5Hex(dto.getPassword() + apUser_DB.getSalt());
            if (!StringUtils.equals(password, apUser_DB.getPassword())) {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }

            //登录成功
            HashMap<String, Object> resultMap = new HashMap<>();
            //用户脱敏处理
            apUser_DB.setPassword("");
            apUser_DB.setSalt("");
            apUser_DB.setPhone("");
            resultMap.put("user", apUser_DB);
            //根据JWT生成token
            Map<String, Object> claimMaps = new HashMap<>();
            claimMaps.put("userId", apUser_DB.getId());
            claimMaps.put("userName", apUser_DB.getName());
            String token = AppJwtUtil.getToken(claimMaps);
            resultMap.put("token", token);
            return ResponseResult.okResult(resultMap);
        } else {
            //2.1 不登录先查看  id=0
            ApUser apUser = new ApUser();
            apUser.setId(0);
            HashMap<String, Object> resultMap = new HashMap<>();
            resultMap.put("user", apUser);
            //根据JWT生成token
            Map<String, Object> claimMaps = new HashMap<>();
            claimMaps.put("userId", apUser.getId());
            claimMaps.put("userName", dto.getEquipmentId());
            String token = AppJwtUtil.getToken(claimMaps);
            resultMap.put("token", token);
            return ResponseResult.okResult(resultMap);
        }
    }
}
